package com.oracle.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.pojo.Admin;
import com.oracle.service.IAdminService;
import com.oracle.util.MD5;
import com.oracle.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
@Api(tags = "管理员操作控制器")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    /**
     * 模糊查询带分页操作
     * http://localhost/sms/adminController/getAllAdmin/1/3
     */
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    @ApiOperation("模糊查询带分页功能")
    public Result getAdminsByConditions(
            @ApiParam("分页条件的页码") @PathVariable("pageNo") Integer pageNo
            ,
            @ApiParam("分页查询的页大小") @PathVariable("pageSize") Integer pageSize
            ,
            @ApiParam("模糊查询条件") String adminName
    ){
        Page<Admin> adminPage = new Page<>(pageNo, pageSize);

        IPage<Admin> ipage = adminService.getAdminsByConditions(adminPage,adminName);

        return Result.ok(ipage);
    }


    /**
     * 管理员的添加或者修改操作
     * saveOrUpdateAdmin
     */
    @PostMapping("/saveOrUpdateAdmin")
    @ApiOperation("管理员增加或者修改操作")
    public Result saveOrUpdate(
            @ApiParam("添加或者修改的管理员对象") @RequestBody Admin admin
    ){
        Integer id = admin.getId();
        if (id == null || id == 0) {
            // 执行添加操作
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        // 执行修改操作
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }
    /**
     * 删除单条记录或者批量删除
     * deleteAdmin
     */
    @DeleteMapping("/deleteAdmin")
    @ApiOperation("删除一个或者删除多个管理员")
    public Result deleteAdmin(
            @ApiParam("删除的id数组") @RequestBody List<Integer> ids
            ){
        adminService.removeByIds(ids);

        return Result.ok();
    }



}
